<?php // 公共函数

/**
 * 生产用户密码
 * @param $safe_key
 * @param $str
 * @return string
 */
function Md5Pass($safe_key,$str){
    return md5($safe_key.trim($str));
}

/**
 * 统一返回数据格式
 * @param $code
 * @param $success
 * @param $msg
 * @param $data
 * @param array $extra
 * @return \Illuminate\Http\JsonResponse
 */
function JsonResponse($code, $success, $msg, $data = [], $extra = []){
        return response()->json([
            'code' => $code,
            'success' => $success,
            'msg' => $msg,
            'data' => $data,
            'extra' => $extra,
        ]);
}

/**
 * 返回图片真实地址
 */
function getImageUrl($list,$key){
    foreach ((object)$list as $v){
        $v->$key = url($v->$key);
    }
    return $list;
}

/**
 * 生成用户安全码
 * @param int $length
 * @param string $char
 * @return bool|string
 */
function createSafeKey($length = 16, $char = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') {
    if(!is_int($length) || $length < 0) {
        return false;
    }

    $string = '';
    for($i = $length; $i > 0; $i--) {
        $string .= $char[mt_rand(0, strlen($char) - 1)];
    }
    return $string;
}

function createToken(){
    return uniqid(time());
}

?>
